/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
osThreadId Flash_taskHandle;
/* USER CODE END Variables */
osThreadId Imu_taskHandle;
osThreadId Log_taskHandle;
osThreadId Main_ctrl_taskHandle;
osThreadId comm_taskHandle;
osThreadId Default_taskHandle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
void flash_task(void const * argument);
/* USER CODE END FunctionPrototypes */

void imu_task(void const * argument);
void log_task(void const * argument);
void main_control_task(void const * argument);
void communicate_task(void const * argument);
void default_task(void const * argument);

extern void MX_USB_DEVICE_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
  *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
  *ppxIdleTaskStackBuffer = &xIdleStack[0];
  *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  /* place for user code */
}
/* USER CODE END GET_IDLE_TASK_MEMORY */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* definition and creation of Imu_task */
  osThreadDef(Imu_task, imu_task, osPriorityRealtime, 0, 256);
  Imu_taskHandle = osThreadCreate(osThread(Imu_task), NULL);

  /* definition and creation of Log_task */
  osThreadDef(Log_task, log_task, osPriorityAboveNormal, 0, 1024);
  Log_taskHandle = osThreadCreate(osThread(Log_task), NULL);

  /* definition and creation of Main_ctrl_task */
  osThreadDef(Main_ctrl_task, main_control_task, osPriorityHigh, 0, 256);
  Main_ctrl_taskHandle = osThreadCreate(osThread(Main_ctrl_task), NULL);

  /* definition and creation of comm_task */
  osThreadDef(comm_task, communicate_task, osPriorityNormal, 0, 1024);
  comm_taskHandle = osThreadCreate(osThread(comm_task), NULL);

  /* definition and creation of Default_task */
  osThreadDef(Default_task, default_task, osPriorityBelowNormal, 0, 256);
  Default_taskHandle = osThreadCreate(osThread(Default_task), NULL);

  /* USER CODE BEGIN RTOS_THREADS */
	osThreadDef(Flash_task, flash_task, osPriorityLow, 0, 2048);
	Flash_taskHandle = osThreadCreate(osThread(Flash_task), NULL);
	
//	vTaskSuspend(Imu_taskHandle);
	vTaskSuspend(Main_ctrl_taskHandle);
	
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

}

/* USER CODE BEGIN Header_imu_task */
/**
  * @brief  Function implementing the Imu_task thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_imu_task */
__weak void imu_task(void const * argument)
{
  /* init code for USB_DEVICE */
  MX_USB_DEVICE_Init();
  /* USER CODE BEGIN imu_task */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END imu_task */
}

/* USER CODE BEGIN Header_log_task */
/**
* @brief Function implementing the Log_task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_log_task */
__weak void log_task(void const * argument)
{
  /* USER CODE BEGIN log_task */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END log_task */
}

/* USER CODE BEGIN Header_main_control_task */
/**
* @brief Function implementing the Main_ctrl_task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_main_control_task */
__weak void main_control_task(void const * argument)
{
  /* USER CODE BEGIN main_control_task */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END main_control_task */
}

/* USER CODE BEGIN Header_communicate_task */
/**
* @brief Function implementing the comm_task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_communicate_task */
__weak void communicate_task(void const * argument)
{
  /* USER CODE BEGIN communicate_task */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END communicate_task */
}

/* USER CODE BEGIN Header_default_task */
/**
* @brief Function implementing the Default_task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_default_task */
__weak void default_task(void const * argument)
{
  /* USER CODE BEGIN default_task */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END default_task */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */
